/**
用于POST提交B-LINK网管型交换机，让它自动重启
前提是这个交换机没有设置用户名和密码登录
<form method="post" action="reboot.cgi">
  <b style="font-weight:normal;font-family: Geneva, Arial, Helvetica, sans-serif;letter-spacing:.45pt">重启交换机.</b>
  <p>
  <input value="  重启  " type="submit">
  <input name="cmd" value="reboot" type="hidden">
</form>

//需要post的数据,以key-value形式提交
data := make(url.Values)
data[“appId”] = []string{“10001”}
data[“appKey”] = []string{“test”}
data[“option”] = []string{“upload”}
data[“sign”] = []string{“1111”}

res, err := http.PostForm("https://u.nuomi.com/platform/boce/openapi/simulator", data)
//设置http中header参数，可以再此添加cookie等值
//res.Header.Add("User-Agent", "***")
//res.Header.Add("http.socket.timeou", 5000)

if err != nil {
    fmt.Println(err.Error())
    return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println("post send success")
fmt.Println(string(body))


**/
package main

import (
	"fmt"
	"net/http"
	"net/url"
)

func main() {
	//这里添加post的body内容
	data := make(url.Values)
	//data["key"] = []string{"cmd"}	//添加提交的值
	data["cmd"] = []string{"reboot"}

	//把post表单发送给目标服务器
	res, err := http.PostForm("http://192.168.1.5/reboot.cgi", data)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	defer res.Body.Close()

	fmt.Println("post send success")
}



/****
如不想用现成的包，那么下面这段代码就是直接通过HTTP协议实现post请求，运行效果和上面这段代码一样：
package main

import (
	"fmt"
	"net"
)

func main() {
	//因为post方法属于HTTP协议，HTTP协议以TCP为基础，所以先建立一个
	//TCP连接，通过这个TCP连接来发送我们的post请求
	conn, err := net.Dial("tcp", "127.0.0.1:80")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	defer conn.Close()

	//构造post请求
	var post string
	post += "POST /postpage HTTP/1.1\r\n"
	post += "Content-Type: application/x-www-form-urlencoded\r\n"
	post += "Content-Length: 37\r\n"
	post += "Connection: keep-alive\r\n"
	post += "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6\r\n"
	post += "\r\n"
	post += "key=this is key&value=this is value\r\n"

	if _, err := conn.Write([]byte(post)); err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println("post send success.")
}

***/